package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"ocane/models"
	"ocane/rpc/consumer"
	"ocane/utils"
	"strconv"
	"strings"
	"time"
)

type MainController struct {
	beego.Controller
}
type UserController struct {
	beego.Controller
}

type AddUserController struct {
	beego.Controller
}

type QueryUserAllController struct {
	beego.Controller
}

type LoginUserController struct {
	beego.Controller
}

func (c *MainController) Get() {
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	userName := c.GetString("userName")
	c.Data["userName"] = userName
	consumer.QueryTest()
	//rpc.InitRpcClient()
	c.TplName = "index.tpl"
}

// Get /* 根据id查询
func (this *UserController) Get()  {
	utils.SetString("userName",this.GetString("userName"))
	getInt, _ := this.GetInt("id")
	queryUser := models.QueryUser(getInt)
	this.Data["json"] = models.Result{Data: queryUser,Code: 200}
	this.ServeJSON()
}

func (this *UserController) QueryUserById()  {
	utils.SetString("userName",this.GetString("userName"))
	getInt, _ := this.GetInt("id")
	queryUser := models.QueryUser(getInt)
	this.Data["json"] = models.Result{Data: queryUser,Code: 200}
	this.ServeJSON()
}

// Get /** 根据IDlist查询用户列表
func (this *QueryUserAllController)Get()  {
	getString := this.GetString("ids")
	split := strings.Split(getString, ",")
	ints := []int{}
	for i := range split {
		atoi, _ := strconv.Atoi(split[i])
		ints = append(ints,atoi)
	}
	list := models.QueryUserList(split)
	this.Data["json"] = models.Result{Data: list,Code: 200}
	this.ServeJSON()
}

// Get /** 添加用户
func (this *AddUserController) Get()  {
	getInt, _ := this.GetInt("id")
	goUser := models.GoUser{Id: getInt, Name: "wangbo", Sex: 2, CreateDate: time.Now(), Age: 10}
	models.AddUser(goUser)
	this.Data["json"] = models.Result{Data: goUser,Code: 200}
	this.ServeJSON()
}

// Get /**此url提供给扫描二维码后请求登陆地址，先用前端直接生成随机字符串给到后端作为登陆依据
func (this *LoginUserController) Get()  {
	getString := this.GetString("loginKey")
	//发送socket请求给web客户端表示登录成功
	utils.LoginKeyChannel<-getString
	this.Data["json"] = models.Result{
		Data: getString,
		Code: 200,
	}
	this.ServeJSON()
}

type User struct {
	Id int `json:"id"`
	Name string `json:"name"`
}

